import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/provider/message_provider.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/badge.dart';
import 'package:xiongan_app_yy/entrys/rp/user_entity.dart';

class HorizontalListHomeWidget extends StatefulWidget {
  final Color background;
  final String title;
  final String badgeText;
  final double fontSize;
  final Color fontColor;
  String route;
  final String image;
  final String modular;
  final Object arguments;
  final double imageHeight;
  final double imageWidth;
  final EdgeInsets margin;

  HorizontalListHomeWidget({
    this.background,
    @required this.title,
    @required this.modular,
    this.margin,
    this.route,
    @required this.image,
    this.fontSize = 20,
    this.imageHeight = 35,
    this.imageWidth = 39,
    this.fontColor = Colors.white,
    this.badgeText = null,
    this.arguments,
  });

  @override
  State<StatefulWidget> createState() {
    return HorizontalListHomeWidgetState();
  }
}

class HorizontalListHomeWidgetState extends State<HorizontalListHomeWidget> {
  Widget image;
  String badgeText;
  bool flag = true; //是否弹无权限toast

  @override
  void initState() {
    badgeText = widget.badgeText;
  }

  @override
  Widget build(BuildContext context) {
    if (badgeText != null) {
      image = Badge(
          text: badgeText,
          child: Container(
            margin: EdgeInsets.all(5),
            height: widget.imageHeight,
            width: widget.imageWidth,
            child: Image.asset(
              widget.image,
              fit: BoxFit.fill,
            ),
          ));
    } else {
      image = Container(
        margin: EdgeInsets.all(5),
        height: widget.imageHeight,
        width: widget.imageWidth,
        child: Image.asset(
          widget.image,
          fit: BoxFit.fill,
        ),
      );
    }
    return GestureDetector(
        child: Container(
          color: widget.background,
          margin: widget.margin,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              image,
              Text(widget.title,
                  style: TextStyle(
                      color: widget.fontColor, fontSize: widget.fontSize))
            ],
          ),
        ),
        onTap: () {
          Provider.of<MessageProvider>(context).getList();
          if (DataSingleton().Data.appModuleMenusList.length == 1) {
            Navigator.pushNamed(context, widget.route,
                arguments: widget.arguments);
          } else if (DataSingleton().Data.appModuleMenusList.length == 3) {
            if (widget.modular == "主菜单") {
              Navigator.pushNamed(context, widget.route,
                  arguments: widget.arguments);
            } else if (widget.modular == "作业") {
              for (int i = 0;
                  i < DataSingleton().Data.appModuleMenusList.length;
                  i++) {
                if (DataSingleton()
                        .Data
                        .appModuleMenusList
                        .elementAt(i)
                        .nodeName ==
                    "作业") {
                  UserAppmodulemenuslistMenutreedto bean = DataSingleton()
                      .Data
                      .appModuleMenusList
                      .elementAt(i)
                      .menuTreeDto;
                  print(bean.toJson().toString());
                  for (int j = 0; j < bean.nezadaTreeList.length; j++) {
                    if (bean.nezadaTreeList.elementAt(j).nodeName ==
                        widget.title) {
                      Navigator.pushNamed(context, widget.route,
                          arguments: widget.arguments);
                      flag = false;
                      break;
                    }
                  }
                  if (flag) {
                    TostUtil.showToast("无权限");
                  }
                }
              }
            } else if (widget.modular == "信息") {
              for (int i = 0; i < DataSingleton().Data.appModuleMenusList.length; i++) {
                if (DataSingleton().Data.appModuleMenusList.elementAt(i).nodeName == "信息") {
                  UserAppmodulemenuslistMenutreedto bean = DataSingleton().Data.appModuleMenusList.elementAt(i).menuTreeDto;
                  for (int j = 0; j < bean.nezadaTreeList.length; j++) {
                    if (bean.nezadaTreeList.elementAt(j).nodeName.contains(widget.title) ) {
                      Navigator.pushNamed(context, widget.route, arguments: widget.arguments);
                      flag = false;
                      break;
                    }
                  }
                  if (flag) {
                    TostUtil.showToast("无权限");
                  }
                }
              }
            }
          }
        });
  }
}
